package com.eve.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public final class DateHelper {

	/**
	 * 把日期转化成指定的日期格式 返回String
	 * 
	 * @param date
	 * @return
	 */
	public static String dateToString(Date date, String format) {
		if (date == null){
			return "";
		}
		SimpleDateFormat df = new SimpleDateFormat(format);
		return df.format(date);
	}

	/**
	 * 把日期转化成指定的日期格式 返回String
	 * 
	 * @param date
	 * @return
	 * @throws
	 */
	public static Date stringToDate(String date, String format) {
		SimpleDateFormat df = new SimpleDateFormat(format);
		try {
			return df.parse(date);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}





	public static Date stringToDateCommon(String date) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return df.parse(date);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}

	public static String dateToStringCommon(Date date) {
		return dateToString(date, "yyyy-MM-dd HH:mm:ss");
	}

	private static String[] weekday_gb = { "星期日", "星期一", "星期二", "星期三", "星期四",
			"星期五", "星期六" };

	// millisecond = String.valueOf(cal.get(Calendar.MILLISECOND));

	public final static String getCurrentWeekDay() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new java.util.Date());
		return weekday_gb[cal.get(Calendar.DAY_OF_WEEK) - 1];
	}

	public final static String getCurrentYear() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new java.util.Date());
		return String.valueOf(cal.get(Calendar.YEAR));
	}

	public final static String getCurrentMonDay() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new java.util.Date());
		String month = String.valueOf(cal.get(Calendar.MONTH) + 1);
		if (month.length() == 1) {
			month = "0" + month;
		}
		String day = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
		if (day.length() == 1) {
			day = "0" + day;
		}
		return month + day;
	}

	public final static String getCurrentYearMonDay() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new java.util.Date());
		String year = String.valueOf(cal.get(Calendar.YEAR));
		String month = String.valueOf(cal.get(Calendar.MONTH) + 1);
		if (month.length() == 1) {
			month = "0" + month;
		}
		String day = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
		if (day.length() == 1) {
			day = "0" + day;
		}
		return year.substring(2) + month + day;
	}

	public final static String getCurrentTime() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new java.util.Date());
		String year = String.valueOf(cal.get(Calendar.YEAR));
		String month = String.valueOf(cal.get(Calendar.MONTH) + 1);
		if (month.length() == 1) {
			month = "0" + month;
		}
		String day = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
		if (day.length() == 1) {
			day = "0" + day;
		}
		String hour = String.valueOf(cal.get(Calendar.HOUR_OF_DAY));
		if (hour.length() == 1) {
			hour = "0" + hour;
		}
		String minute = String.valueOf(cal.get(Calendar.MINUTE));
		if (minute.length() == 1) {
			minute = "0" + minute;
		}
		String second = String.valueOf(cal.get(Calendar.SECOND));
		if (second.length() == 1) {
			second = "0" + second;
		}
		return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":"
				+ second;
	}

	public final static String getLastYearMon() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new java.util.Date());
		String month = String.valueOf(cal.get(Calendar.MONTH) + 1);
		if (month.length() == 1) {
			month = "0" + month;
		}
		if (Integer.parseInt(month) == 1) {
			return getLastYear() + "12";
		} else {
			String year = String.valueOf(cal.get(Calendar.YEAR));
			String lastMonth = String.valueOf(Integer.parseInt(month) - 1);
			if (lastMonth.length() == 1) {
				lastMonth = "0" + lastMonth;
			}
			return year + lastMonth;
		}
	}

	public final static String getLastYear() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new java.util.Date());
		String year = String.valueOf(cal.get(Calendar.YEAR));
		int lastYear = Integer.parseInt(year) - 1;
		return String.valueOf(lastYear);
	}

	public final static String getCurrentMonth() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new java.util.Date());
		String month = String.valueOf(cal.get(Calendar.MONTH) + 1);
		if (month.length() == 1) {
			month = "0" + month;
		}
		return month;
	}

	public final static String getCurrentDay() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new java.util.Date());
		String day = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
		if (day.length() == 1) {
			day = "0" + day;
		}
		return day;
	}

	public final static String getCurrentYearMon() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new java.util.Date());
		String year = String.valueOf(cal.get(Calendar.YEAR));
		String month = String.valueOf(cal.get(Calendar.MONTH) + 1);
		if (month.length() == 1) {
			month = "0" + month;
		}
		return year + month;
	}

	public final static String getCurrentDate() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new java.util.Date());
		String year = String.valueOf(cal.get(Calendar.YEAR));
		String month = String.valueOf(cal.get(Calendar.MONTH) + 1);
		if (month.length() == 1) {
			month = "0" + month;
		}
		String day = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
		if (day.length() == 1) {
			day = "0" + day;
		}
		return year + "." + month + "." + day;
	}

	public final static String getCurrentDateGBK() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new java.util.Date());
		String year = String.valueOf(cal.get(Calendar.YEAR));
		String month = String.valueOf(cal.get(Calendar.MONTH) + 1);
		if (month.length() == 1) {
			month = "0" + month;
		}
		String day = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
		if (day.length() == 1) {
			day = "0" + day;
		}
		return year + "年" + month + "月" + day + "日";
	}

	private static SimpleDateFormat ndf = new SimpleDateFormat(
			"yyyyMMddHHmmssSSS");

	public static String genDateId() {
		Random r = new Random();
		return ndf.format(new Date()) + r.nextInt(10) + r.nextInt(10)
				+ r.nextInt(10) + r.nextInt(10);
	}



	public static Date parseDate(String datestr) {
		if (datestr != null && datestr.length() >= 19) {
			// datestr = datestr.substring(0,19);
			SimpleDateFormat df = new SimpleDateFormat(
					"yyyy-MM-dd'T'HH:mm:ss.Sz");
			try {
				return df.parse(datestr);
			} catch (ParseException e) {
				e.printStackTrace();
				throw new RuntimeException(e.getMessage());
			}
		}
		throw new RuntimeException(
				"日期格式不规范，必须为类似[2007-06-16T11:03:09.000+08:00]的格式");
	}
	/**
	 * @param date 日期
	 * @param otherDate 另一个日期
	 * @return 相差天数
	 */
	public static Long getIntervalDays(Date date, Date otherDate) {
		long time = Math.abs(date.getTime() - otherDate.getTime());
		return time /(24 * 60 * 60 * 1000);
	}


	public static Date addDay(Date date, int amount) {

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, amount);
		return calendar.getTime();
	}

	public static void main(String[] args) {
		System.out.println(getIntervalDays(stringToDate("20151013 00:00:00", "yyyyMMdd HH:mm:ss"),
				new Date()));
//
//		System.out.println(dateToString(addDay(new Date(), 5), "yyyy-MM-dd HH:mm:ss"));
//		Preconditions.checkArgument(1<0, "距离您付款时间2015-11-05 22:29:57超过3天，已经不能退款");

	}


    public static Date parseGMT(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR, -14);
        return calendar.getTime();

    }
}
